package com.mall.weapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mall.weapp.mapper.CategoryMapper;
import com.mall.weapp.mapper.ProductMapper;
import com.mall.weapp.model.entity.Category;
import com.mall.weapp.model.entity.Product;
import com.mall.weapp.model.vo.CateporyVo;
import com.mall.weapp.model.vo.ProductListVo;
import com.mall.weapp.service.CateporyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CateporyServiceImpl implements CateporyService {
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private CategoryMapper cateporyMapper;

    @Override
    public List<CateporyVo> cateporys() {
        List<Category> categories = cateporyMapper.selectList(null);
        ArrayList<CateporyVo> cateporyList = new ArrayList<>();
        for (Category category : categories) {
            cateporyList.add(new CateporyVo(category));
        }
        return cateporyList;
    }

    @Override
    public List<ProductListVo> getProductByCategoryId(long id) {
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper();
        wrapper.eq(Product::getStatus, 1);
        wrapper.eq(Product::getCategoryId, id);
        List<Product> products = productMapper.selectList(wrapper);
        List<ProductListVo> productList = new ArrayList<>();
        for (Product product : products) {
            productList.add(new ProductListVo(product));
        }
        return productList;
    }
}
